import { Node, UITransform, Layers, SpriteFrame } from 'cc';

export const createUINode = (name:string = '') => {
  const node = new Node(name)
  const transform = node.addComponent(UITransform)
  // 左上角为原点
  transform.setAnchorPoint(0,1)

  // 每个元素想要被 canvas 渲染的话，必须要有 layer 属性
  node.layer = 1 << Layers.nameToLayer('UI_2D')
  // 调整图片的位置
  return node
}

// 返回随机数
export const randomByRange = (start:number, end:number) => {
  return Math.floor(start + (end - start) * Math.random())
}
// 将资源图片排序
const reg = /\((\d+)\)/
const getNumberWithinString = (str:string) => {return parseInt(str.match(reg)[1] || '0')}

export const sortSprintFrame = (spriteFrames:SpriteFrame[]) =>
  spriteFrames.sort((a,b) => getNumberWithinString(a.name) - getNumberWithinString(b.name))

